package org.mobiletrain.www.secondproject2;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 登录页
 * @author 张英明
 */

public class UserLoginActivity extends ActionBarActivity {

    private String mRph;
    private String mRpwd;
    private EditText mEdphone;
    private EditText mEdpassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        userRegister();
        userLogin();


    }

    private void userRegister(){
        Button register = (Button) findViewById(R.id.btn_userLogin_register);
        mEdphone = (EditText) findViewById(R.id.edtTxt_userLogin_phone);
        mEdpassword = (EditText) findViewById(R.id.edtTxt_userLogin_password);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRph = String.valueOf(mEdphone.getText());
                mRpwd = String.valueOf(mEdpassword.getText());
                SharedPreferences.Editor editor = getSharedPreferences("useraccount", MODE_APPEND).edit();
                editor.putString("phone", mRph);
                editor.putString("pwd",mRpwd);
                editor.commit();
            }
        });
    }

    private void userLogin(){
        Button login = (Button) findViewById(R.id.btn_userLogin_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences spf = getSharedPreferences("useraccount", MODE_APPEND);
                String phone = spf.getString("phone", "默认电话号");
                String pwd = spf.getString("pwd","默认密码");
                String inphone = String.valueOf(mEdphone.getText());
                String inpwd = String.valueOf(mEdpassword.getText());
                if (phone!=null && pwd!=null && inphone!=null && inpwd !=null){
                    if (phone.contains(inphone) && pwd.contains(inpwd)){
                        Toast.makeText(UserLoginActivity.this,phone+"登录成功!",Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(UserLoginActivity.this,"登录失败!",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(UserLoginActivity.this,"请正确输入用户名密码!",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

}
